package com.itheima.recursion_single;

/**
 * @FileName E05InsertionSort
 * @Description
 * @Author Sword
 * @date 2025-03-11
 **/
public class E05InsertionSort {

    public static void sort(int[] a) {
        insertion(a, 1);
    }

    /*private static void insertion(int[] a, int low) {
        if (low == a.length) {
            return;
        }

        int t = a[low];     //待插入值
        int i = low - 1;    //已排序区指针
        while (i >= 0 && a[i] > t) {  //没找到插入位置
            a[i + 1] = a[i];//空出插入位置
            i--;
        }

        if (i != low) {
            a[i + 1] = t;//找到插入位置
        }
        insertion(a, low + 1);
    }*/

    private static void insertion(int[] a, int low) {
        if (low == a.length) {
            return;
        }

        int i = low - 1;    //已排序区指针
        while (i >= 0 && a[i] > a[i + 1]) {  //没找到插入位置
            int t = a[i];
            a[i] = a[i + 1];//空出插入位置
            a[i + 1] = t;
            i--;
        }


        insertion(a, low + 1);
    }
}
